package com.enjoy.rpc.rpc;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * Created by HP on 2019/7/3.
 */
public class ServerSubcri {
    public static String findService(String serverName) throws IOException {
        Socket socket = null;

        ObjectOutputStream outputStream = null;
        ObjectInputStream inputStream = null;


        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 10002);

        socket = new Socket();
        try {
            socket.connect(address);
            outputStream = new ObjectOutputStream(socket.getOutputStream());
            inputStream = new ObjectInputStream(socket.getInputStream());
            outputStream.writeUTF("Q");
            outputStream.writeUTF(serverName);
            outputStream.flush();
           return inputStream.readUTF();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                socket.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return "F";
    }
}
